/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.sql.log;

import com.terracloud.server.common.data.log.LogLevel;
import com.terracloud.server.common.data.log.LogType;
import com.terracloud.server.common.data.log.PlatformLog;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.common.data.id.UserId;
import com.terracloud.server.common.data.id.PlatformLogId;
import com.terracloud.server.common.data.page.PageData;
import com.terracloud.server.common.data.page.TimePageLink;
import com.terracloud.server.dao.DaoUtil;
import com.terracloud.server.dao.log.PlatformLogDao;
import com.terracloud.server.dao.model.sql.PlatformLogEntity;
import com.terracloud.server.dao.sql.JpaAbstractDao;
import com.terracloud.server.dao.util.SqlDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Component
@Slf4j
@SqlDao
public class JpaPlatformLogDao extends JpaAbstractDao<PlatformLogEntity, PlatformLog> implements PlatformLogDao {

    @Autowired
    private PlatformLogRepository platformLogRepository;

    @Override
    protected Class<PlatformLogEntity> getEntityClass() {
        return PlatformLogEntity.class;
    }

    @Override
    protected JpaRepository<PlatformLogEntity, UUID> getRepository() {
        return platformLogRepository;
    }

    @Override
    @Transactional
    public PlatformLog save(PlatformLog platformLog) {
        if (platformLog.getTenantId() != null) {
            return save(platformLog.getTenantId(), platformLog);
        } else {
            // 如果没有租户ID，则创建一个默认的TenantId
            return save(TenantId.SYS_TENANT_ID, platformLog);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public PlatformLog findById(PlatformLogId platformLogId) {
        // 使用系统租户ID作为默认值
        return findById(TenantId.SYS_TENANT_ID, platformLogId.getId());
    }

    @Override
    @Transactional
    public void removeById(PlatformLogId platformLogId) {
        // 使用系统租户ID作为默认值
        removeById(TenantId.SYS_TENANT_ID, platformLogId.getId());
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantId(TenantId tenantId, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantId(
                        tenantId.getId(), 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantIdAndUserId(TenantId tenantId, UserId userId, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantIdAndUserId(
                        tenantId.getId(), 
                        userId.getId(), 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantIdAndType(TenantId tenantId, LogType logType, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantIdAndType(
                        tenantId.getId(), 
                        logType, 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantIdAndLevel(TenantId tenantId, LogLevel logLevel, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantIdAndLevel(
                        tenantId.getId(), 
                        logLevel, 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantIdAndUserIdAndType(TenantId tenantId, UserId userId, LogType logType, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantIdAndUserIdAndType(
                        tenantId.getId(), 
                        userId.getId(), 
                        logType, 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByTenantIdAndUserIdAndLevel(TenantId tenantId, UserId userId, LogLevel logLevel, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByTenantIdAndUserIdAndLevel(
                        tenantId.getId(), 
                        userId.getId(), 
                        logLevel, 
                        DaoUtil.toPageable(pageLink)));
    }

    @Override
    @Transactional(readOnly = true)
    public long countByStatus(TenantId tenantId, String status) {
        return platformLogRepository.countByStatus(tenantId.getId(), status);
    }

    @Override
    @Transactional(readOnly = true)
    public long countBySensitiveOperation(TenantId tenantId, boolean isSensitive) {
        if (isSensitive) {
            return platformLogRepository.countBySensitiveOperation(tenantId.getId());
        } else {
            // For non-sensitive operations, we would need a different query
            // This is a simplified implementation
            return 0;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public PageData<PlatformLog> findByBusinessId(TenantId tenantId, String businessId, TimePageLink pageLink) {
        return DaoUtil.toPageData(
                platformLogRepository.findByBusinessId(
                        tenantId.getId(), 
                        businessId, 
                        DaoUtil.toPageable(pageLink)));
    }
}